package cn.zyx.org.service.impl;

import cn.zyx.basic.exception.BusinessException;
import cn.zyx.basic.service.impl.BaseServiceImpl;
import cn.zyx.org.domain.Employee;
import cn.zyx.org.domain.Shop;
import cn.zyx.org.mapper.EmployeeMapper;
import cn.zyx.org.mapper.ShopMapper;
import cn.zyx.org.service.IEmployeeService;
import cn.zyx.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通过继承基础接口的实现类，来得到基础接口的实现
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)  // 加上事务
public class ShopServiceImpl extends BaseServiceImpl<Shop>
        implements IShopService {

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    IEmployeeService employeeService;

    @Override
    @Transactional
    public void settledIn(Shop shop) {
        // 获取用户输入的员工信息
        Employee admin = shop.getAdmin();
        // 先校验有没有被注册过 --> username,phone,email
        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        // 如果有，提示用户已经入驻过了，请直接登录 -- 如果忘记密码，请找回密码
        if (employee != null){ // 不为空表示已经入驻过了
            throw new BusinessException("您已经入驻过了，请直接登录");
        }
        // 如果没有,先保存员工数据，保存成功返回自增id
        // employeeMapper.save(admin);
        // 店铺入驻，操作员工和店铺的信息的同时，同步操作登录信息表中的数据
        employeeService.add(admin);
        // 保存门店数据 - 管理员id - 返回门店自增id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        // 修改管理员门店id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
